#ifndef AGENTTASKBASE_H
#define AGENTTASKBASE_H

#include <QObject>
#include <QMap>
#include <QHostAddress>
class AgentProtocol;
class QUdpSocket;
class AgentPush;

class AgentTaskBase : public QObject
{
    Q_OBJECT
public:
    explicit AgentTaskBase(QMap <QByteArray,QByteArray> mapIPPortReqPacket,QByteArray packet,
          QString paramAppIP,quint16 paramAppPort,QObject *parent = 0);
    virtual ~AgentTaskBase();
signals:
    void sigSendDataToApp(QByteArray packet,QString paramAppIP,quint16 paramAppPort);
    void sigErrorInfo(QByteArray errorInfo);

    void sigPushToApps(QByteArray packet);//将消息推送给APP
    void sigNewSubscriber(QByteArray baSN,QByteArray baIPPort,QByteArray origReqPacket);//新增一个订阅者
    void sigSetIPAnalyzeIncomeDatagram( QByteArray datagram,   QHostAddress parClientIP,
        quint16 parClientPort); //设置clientip和clientport，然后调用analyzeIncomeDatagram
private slots:
    void slotEmitSigPushToApps(); //将私有成员packetToPush发射给agentpush
    virtual void slotInitBaseClass();
    virtual void slotInitDerivedClass()=0;//由派生类实现

    virtual void slotAgentDelete(QByteArray packet,QString paramAppIP,quint16 paramAppPort);
    virtual void slotAgentGet(QByteArray packet,QString paramAppIP,quint16 paramAppPort);
    //virtual void slotAgentPut(QByteArray packet,QString appIP,quint16 appPort)=0;
    virtual void slotAgentWatch(QByteArray packet,QString paramAppIP,quint16 paramAppPort);

    virtual void slotProcessServerUdpDatagram();
protected:
    AgentPush *agentPush;
    bool isCreatedSuccessfully; //记录是否成功创建
    virtual void analyzeIncomeDatagram(QByteArray packet)=0;
    virtual void slotSendPacket(QByteArray packet,QString strIP,quint16 udpPort);
    virtual bool isComparisonHit(QByteArray baComparingNumber);

    quint64 sNClient; //请求服务的client的包中的SN
    bool isSNClientValid; //

    QByteArray fieldMethod; //put,post,delete
    QByteArray fieldURL; // 如“/DataThreshTrigger/SendPacket”

    QByteArray iOTVersionAppPacket;

    qint64 fieldSN;
    QByteArray fieldHost;
    bool    fieldIsSingleShot;
    qint64  fieldHoldMilliSeonds;
    quint32 fieldHoldTimes;

    QByteArray fieldTriggerDate,fieldTriggerTime;

    qint64 fieldTimeOutSeconds;
    QByteArray fieldComparisonOperator;

    //被比较的数据可能是整数或浮点数
    qint64 fieldComparedNumberInt;
    double fieldComparedNumberDouble;
    bool  fieldIsComparedNumberInt;

    qint64 fieldSrcMac,fieldDestMac;
    QByteArray  fieldSrcNodeName,fieldSrcSensorName,fieldDestSensorName;

    QString appIP;
    quint16 appPort;

    QByteArray origPacket;
    QByteArray fieldPayLoad;

    AgentProtocol* agentProtocol;
    QUdpSocket *udpSocketWithServer;


private:
    QByteArray packetToPush;
//记录在这个task生成之前收到的watch请求。先查看这些里面有没有符合自己情况的
    QMap <QByteArray,QByteArray> mapOldIPPortWatchReqPacket;
    bool compareDouble(double comparingNumber);
    bool compareInt(qint64 comparingNumber);


    //解析某一行，根据内容设置数据成员的值
    bool parseOneLine(QByteArray baLine);
    void setPayLoad(QByteArray paramLoad);

    bool isHit(QByteArray paramPacket);
    //判断某一行的内容是否与当前匹配
    bool isOneLineHit(QByteArray baLine);

    QByteArray getPayLoadFromPacket(QByteArray paramPacket);

};
#endif // AGENTTASKBASE_H
